/**
 * 后端框架模板配置
 * 参考 Knife4j 的设计理念，支持多种主流后端框架
 */

export interface BackendFramework {
  id: string
  name: string
  description: string
  language: string
  version: string
  features: string[]
  dependencies: FrameworkDependency[]
  templates: FrameworkTemplate[]
}

export interface FrameworkDependency {
  name: string
  version: string
  type: "runtime" | "development" | "optional"
  description: string
}

export interface FrameworkTemplate {
  name: string
  path: string
  type: "controller" | "service" | "model" | "config" | "doc"
  description: string
}

/**
 * 支持的后端框架列表
 */
export const backendFrameworks: BackendFramework[] = [
  {
    id: "spring-boot",
    name: "Spring Boot",
    description: "Java 企业级 Web 框架，集成 Knife4j 文档生成",
    language: "Java",
    version: "3.2.0",
    features: [
      "RESTful API",
      "JPA 数据访问",
      "Knife4j 文档生成",
      "Spring Security 安全",
      "Redis 缓存支持",
      "MySQL/PostgreSQL 数据库",
      "Docker 容器化"
    ],
    dependencies: [
      {
        name: "spring-boot-starter-web",
        version: "3.2.0",
        type: "runtime",
        description: "Web 开发基础依赖"
      },
      {
        name: "spring-boot-starter-data-jpa",
        version: "3.2.0",
        type: "runtime",
        description: "JPA 数据访问"
      },
      {
        name: "knife4j-openapi3-jakarta-spring-boot-starter",
        version: "4.3.0",
        type: "runtime",
        description: "Knife4j API 文档生成"
      },
      {
        name: "mysql-connector-j",
        version: "8.2.0",
        type: "runtime",
        description: "MySQL 数据库驱动"
      }
    ],
    templates: [
      {
        name: "Controller",
        path: "/templates/spring-boot/controller.hbs",
        type: "controller",
        description: "REST 控制器模板"
      },
      {
        name: "Service Interface",
        path: "/templates/spring-boot/service.hbs",
        type: "service",
        description: "业务逻辑服务接口模板"
      },
      {
        name: "Service Implementation",
        path: "/templates/spring-boot/service-impl.hbs",
        type: "service",
        description: "业务逻辑服务实现模板"
      },
      {
        name: "Entity",
        path: "/templates/spring-boot/entity.hbs",
        type: "model",
        description: "MyBatis Plus 实体模板"
      },
      {
        name: "DTO",
        path: "/templates/spring-boot/dto.hbs",
        type: "model",
        description: "数据传输对象模板"
      },
      {
        name: "VO",
        path: "/templates/spring-boot/vo.hbs",
        type: "model",
        description: "视图对象模板"
      },
      {
        name: "Mapper Interface",
        path: "/templates/spring-boot/mapper.hbs",
        type: "service",
        description: "MyBatis Mapper 接口模板"
      },
      {
        name: "Mapper XML",
        path: "/templates/spring-boot/mapper.xml.hbs",
        type: "config",
        description: "MyBatis Mapper XML 模板"
      },
      {
        name: "Result",
        path: "/templates/spring-boot/result.hbs",
        type: "model",
        description: "统一返回结果模板"
      },
      {
        name: "PageResult",
        path: "/templates/spring-boot/page-result.hbs",
        type: "model",
        description: "分页返回结果模板"
      },
      {
        name: "ErrorCode",
        path: "/templates/spring-boot/error-code.hbs",
        type: "model",
        description: "错误码枚举模板"
      },
      {
        name: "Knife4j Config",
        path: "/templates/spring-boot/knife4j-config.hbs",
        type: "config",
        description: "Knife4j 配置类"
      },
      {
        name: "POM XML",
        path: "/templates/spring-boot/pom.xml.hbs",
        type: "config",
        description: "Maven 项目配置文件"
      },
      {
        name: "Application YAML",
        path: "/templates/spring-boot/application.yml.hbs",
        type: "config",
        description: "Spring Boot 配置文件"
      }
    ]
  },
  {
    id: "express-js",
    name: "Express.js",
    description: "Node.js 快速 Web 框架，集成 Swagger 文档",
    language: "JavaScript/TypeScript",
    version: "4.18.0",
    features: [
      "RESTful API",
      "Mongoose ODM",
      "Swagger UI 文档",
      "JWT 认证",
      "Redis 缓存",
      "MongoDB 数据库",
      "PM2 进程管理",
      "Docker 容器化"
    ],
    dependencies: [
      {
        name: "express",
        version: "^4.18.0",
        type: "runtime",
        description: "Express 框架"
      },
      {
        name: "mongoose",
        version: "^7.6.0",
        type: "runtime",
        description: "MongoDB ODM"
      },
      {
        name: "swagger-ui-express",
        version: "^5.0.0",
        type: "runtime",
        description: "Swagger UI 集成"
      },
      {
        name: "swagger-jsdoc",
        version: "^6.2.0",
        type: "runtime",
        description: "JSDoc 转 Swagger"
      }
    ],
    templates: [
      {
        name: "Controller",
        path: "/templates/express/controller.hbs",
        type: "controller",
        description: "Express 控制器模板"
      },
      {
        name: "Service",
        path: "/templates/express/service.hbs",
        type: "service",
        description: "业务逻辑服务模板"
      },
      {
        name: "Model",
        path: "/templates/express/model.hbs",
        type: "model",
        description: "Mongoose 模型模板"
      },
      {
        name: "Swagger Config",
        path: "/templates/express/swagger-config.hbs",
        type: "config",
        description: "Swagger 配置文件"
      }
    ]
  },
  {
    id: "fastapi",
    name: "FastAPI",
    description: "Python 现代 Web 框架，自动生成 OpenAPI 文档",
    language: "Python",
    version: "0.104.0",
    features: [
      "RESTful API",
      "SQLAlchemy ORM",
      "自动 OpenAPI 文档",
      "OAuth2 认证",
      "Redis 缓存",
      "PostgreSQL 数据库",
      "Docker 容器化"
    ],
    dependencies: [
      {
        name: "fastapi",
        version: "^0.104.0",
        type: "runtime",
        description: "FastAPI 框架"
      },
      {
        name: "sqlalchemy",
        version: "^2.0.0",
        type: "runtime",
        description: "SQLAlchemy ORM"
      },
      {
        name: "uvicorn",
        version: "^0.24.0",
        type: "runtime",
        description: "ASGI 服务器"
      },
      {
        name: "psycopg2-binary",
        version: "^2.9.0",
        type: "runtime",
        description: "PostgreSQL 驱动"
      }
    ],
    templates: [
      {
        name: "Router",
        path: "/templates/fastapi/router.hbs",
        type: "controller",
        description: "FastAPI 路由模板"
      },
      {
        name: "Service",
        path: "/templates/fastapi/service.hbs",
        type: "service",
        description: "业务逻辑服务模板"
      },
      {
        name: "Model",
        path: "/templates/fastapi/model.hbs",
        type: "model",
        description: "SQLAlchemy 模型模板"
      },
      {
        name: "Schema",
        path: "/templates/fastapi/schema.hbs",
        type: "model",
        description: "Pydantic Schema 模板"
      }
    ]
  }
]

/**
 * 根据框架 ID 获取框架配置
 */
export function getFrameworkById(id: string): BackendFramework | undefined {
  return backendFrameworks.find(framework => framework.id === id)
}

/**
 * 获取支持的编程语言列表
 */
export function getSupportedLanguages(): string[] {
  return [...new Set(backendFrameworks.map(f => f.language))]
}

/**
 * 代码生成配置
 */
export interface CodeGenerationConfig {
  framework: BackendFramework
  projectName: string
  packageName: string
  basePackage: string
  author: string
  version: string
  description: string
  database: {
    type: "mysql" | "postgresql" | "mongodb"
    host: string
    port: number
    database: string
    username: string
    password: string
  }
  redis: {
    host: string
    port: number
    password: string
    database: number
    timeout: number
  }
  jwt: {
    secret: string
    expiration: number
    refreshExpiration: number
  }
  features: {
    enableSwagger: boolean
    enableSecurity: boolean
    enableCache: boolean
    enableDocker: boolean
  }
}
